package org.snail.other.service.redis;

import lombok.extern.log4j.Log4j;
import org.snail.common.dingding.utils.JedisUtils;
import org.snail.common.xmlBean.Group;
import org.snail.common.xmlBean.GroupClassification;
import org.snail.constant.CacheNameConstant;
import org.snail.constant.ErrCodeConstant;
import org.snail.exception.BaseException;
import org.snail.exception.GroupException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Log4j
public class GroupRedis {

    @Autowired
    private JedisUtils jedisUtils;

    /**
     * 初始化数据库中的信息 用做测试
     */
    public void initGroupClassificationList() throws BaseException {
        List<Group> groupList = new ArrayList<>();
        groupList.add(new Group("hookUrl1", "test1"));
        groupList.add(new Group("hookUrl2", "test2"));

        List<GroupClassification> groupClassificationList = new ArrayList<>();
        groupClassificationList.add(new GroupClassification("分组一", groupList));
        groupClassificationList.add(new GroupClassification("分组二", groupList));

        for (GroupClassification groupClassification : groupClassificationList) {
            jedisUtils.saveHashCache("groupClassification", groupClassification.getName(), groupClassification);
        }
    }

    /**
     * 读取redis中的数据
     *
     * @return
     * @throws BaseException
     */
//
//    public List<GroupClassification> getGroupClassificationList() throws BaseException {
//        List<GroupClassification> groupClassificationList = new ArrayList<>();
//
//        Set<String> list = (Set<String>)jedisUtils.getKeysForHash("groupClassification");
//        for(String ClassificationName : list){
//            GroupClassification groupClassification = (GroupClassification) jedisUtils.getHashCache("groupClassification",ClassificationName);
//            groupClassificationList.add(groupClassification);
//        }
//        return groupClassificationList;
//    }
    public Map getGroupClassificationList() throws BaseException {
        Map<String, List> m = jedisUtils.getAllHashCache(CacheNameConstant.GROUP_CLASSIFICATION);
        return m;
    }

    /**
     * 删除一个分组
     */
    public void deleteClassification(String classificationName) throws BaseException {
        Map<String, List> groupClassificationList = getGroupClassificationList();
        for (String name : groupClassificationList.keySet()) {
            if (classificationName == null) {
                log.error("删除分组失败没有分组参数");
                throw new GroupException(ErrCodeConstant.ERROR_CODE_NO_CLASSIFICATIONNAME, "没有群分类名称");
            }
            if (classificationName.equals(name)) {
                jedisUtils.deleteKey(CacheNameConstant.GROUP_CLASSIFICATION, classificationName);
                break;
            }
        }

    }

    /**
     * 在分组中保存一个群
     *
     * @param group
     */
    public void saveGroupClassification(Group group) throws BaseException {
        boolean isAddGroup = false;
        Map<String, List> groupClassificationList = getGroupClassificationList();

        //判断前端传递的分组名字是否在redis中存在 如果存在就往此分组中添加群
        for (String groupClassificationName : groupClassificationList.keySet()) {
            if (group.getGroupClassificationName().equals(groupClassificationName)) {
                groupClassificationList.get(groupClassificationName).add(group);
                isAddGroup = true;
                break;
            }
        }
        //如果不在分组 就新建分组
        if (!isAddGroup) {
            List<Group> groupList = new ArrayList<>();
            groupList.add(group);
            groupClassificationList.put(group.getGroupClassificationName(), groupList);
        }
        for (String groupClassificationName : groupClassificationList.keySet()) {
            jedisUtils.saveHashCache(CacheNameConstant.GROUP_CLASSIFICATION, groupClassificationName, groupClassificationList.get(groupClassificationName));
        }

    }
}
